{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy函数库中存在两种不同的数据类型（矩阵matrix和数组array） ， 都可以用于处理行列表示的数字元素。 虽然它们看起来很相似， 但是在这两个数据类型上执行相同的数学运算可能得到不同的结果， 其中NumPy函数库中的matrix与MATLAB中matrices等价。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 0.65542392, -1.27448963,  0.83859881,  0.03302342],\n",
       "        [ 3.00222389,  1.02903725, -0.48299055, -0.97042876],\n",
       "        [-3.36789007,  1.263969  ,  0.857043  ,  0.69016509],\n",
       "        [ 0.57651206, -0.10480779, -1.06251226,  0.89892858]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "randMat = np.mat(np.random.rand(4,4))\n",
    "\n",
    "# .I 矩阵求逆\n",
    "randMat.I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  1.00000000e+00,  -6.21319529e-17,  -3.47816243e-17,\n",
       "          -2.09244981e-17],\n",
       "        [ -6.07979676e-17,   1.00000000e+00,  -6.18315418e-18,\n",
       "          -6.09071782e-17],\n",
       "        [ -6.41752464e-17,  -1.25662052e-16,   1.00000000e+00,\n",
       "           1.75706968e-18],\n",
       "        [  1.63174271e-16,  -2.35504844e-16,  -1.87026017e-16,\n",
       "           1.00000000e+00]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵乘法\n",
    "invRandMat = randMat.I\n",
    "randMat*invRandMat     #得到的结果近似单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[  2.22044605e-16,  -6.21319529e-17,  -3.47816243e-17,\n",
       "          -2.09244981e-17],\n",
       "        [ -6.07979676e-17,  -1.11022302e-16,  -6.18315418e-18,\n",
       "          -6.09071782e-17],\n",
       "        [ -6.41752464e-17,  -1.25662052e-16,  -2.22044605e-16,\n",
       "           1.75706968e-18],\n",
       "        [  1.63174271e-16,  -2.35504844e-16,  -1.87026017e-16,\n",
       "           0.00000000e+00]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 矩阵减法\n",
    "myEye = randMat*invRandMat\n",
    "myEye - np.eye(4)       #函数eye(4)创建4×4的单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.39140498  0.34745493  0.13321788  0.25843255]\n",
      " [ 0.01555418  0.46305577  0.44305112  0.15915677]\n",
      " [ 0.87908985  0.41931639  0.54517792  0.00180588]\n",
      " [ 0.78985614  0.32677646  0.6106066   0.96738515]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵除法\n",
    "x = np.linalg.solve(myEye,randMat)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
